package com.xfly.app.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.view.AddressPickTask;
import com.ptmall.app.view.NoScrollListView;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.adapter.YfAdapter;
import com.xfly.app.bean.HdBean;
import com.xfly.app.bean.YfBean;
import com.xfly.app.ui.dialog.YfDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.qqtheme.framework.entity.City;
import cn.qqtheme.framework.entity.County;
import cn.qqtheme.framework.entity.Province;

public class YfSetActivity extends BaseMvpActivity {

    TitleBarView title_bar;

    LinearLayout add;

    YfAdapter yfAdapter;

    List<YfBean> list = new ArrayList<>();
    EditText price;
    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    TextView addaddress;
    NoScrollListView listview;
    YfDialog yfDialog;
    TextView set;
    HdBean mHdBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_yfset);
    }

    @Override
    public void initViews() {
        title_bar = findViewById(R.id.title_bar);
        price = findViewById(R.id.price);
        listview = findViewById(R.id.listview);
        add = findViewById(R.id.add);
        addaddress = findViewById(R.id.addaddress);
        set = findViewById(R.id.set);


    }

    @Override
    public void initEvents() {
        title_bar.setText("运费设置");
        title_bar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setData();
            }
        });
        yfAdapter = new YfAdapter(getContext(),list);
        listview.setAdapter(yfAdapter);
        addaddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AddressPickTask task = new AddressPickTask(getActivity());
                task.setHideProvince(false);
                task.setHideCounty(true);
                task.setCallback(new AddressPickTask.Callback() {
                    @Override
                    public void onAddressInitFailed() {
                        showMsg("数据初始化失败");
                    }

                    @Override
                    public void onAddressPicked(Province province, City city, County county) {
                        yfDialog = new YfDialog(getContext());
                        yfDialog.show();
                        yfDialog.setonclick(new YfDialog.Onclick() {
                            @Override
                            public void Yes(String pass) {
                                Map<String,String> map = new HashMap<>();
                                map.put("province_id",province.getAreaId());
                                map.put("city_id",city.getAreaId());
                                map.put("fee",pass);
                                apiDataRepository.yfset(map, new ApiNetResponse<Object>() {
                                    @Override
                                    public void onSuccess(Object data) {
                                        showMsg("添加成功");
                                        getyf();
                                        yfDialog.dismiss();
                                    }
                                });
                            }
                        });
                    }
                });
                task.execute("山东省", "临沂市");
            }
        });
        getyf();
        getHd();
    }

    private void getHd(){
        apiDataRepository.gethd(null, new ApiNetResponse<HdBean>() {
            @Override
            public void onSuccess(HdBean data) {
                mHdBean = data;
            }

        });
    }

    private void setData(){
        Map<String,String> map = new HashMap<>();
        map.put("free_shipping",mHdBean.free_shipping);
        map.put("postage",price.getText().toString());
        map.put("full",mHdBean.full);
        map.put("full_reduce",mHdBean.full_reduce);
        apiDataRepository.setHd(map, new ApiNetResponse<Object>(getContext()) {
            @Override
            public void onSuccess(Object data) {
                showMsg("修改成功");
                finish();
            }
        });
    }

    private void getyf(){
        apiDataRepository.getyf(null, new ApiNetResponse<List<YfBean>>() {
            @Override
            public void onSuccess(List<YfBean> data) {
                list = data;
                yfAdapter.pushData(list);
//                if(list.size()>0){
                    add.setVisibility(View.GONE);
//                }
            }
        });
        apiDataRepository.gethd(null, new ApiNetResponse<HdBean>() {
            @Override
            public void onSuccess(HdBean data) {
                price.setText(data.postage);
            }
        });
    }
}
